package com.gosophia.metadataEngine.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.DateTranslate;
import com.gosophia.commons.Page;
import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.metadataEngine.bean.PageShowBean;
import com.gosophia.metadataEngine.dto.MetaDataFieldDto;
import com.gosophia.metadataEngine.ws.MetadataEngineWebService;
/**
 * 分页显示业务数据
 * @author wangxinbing
 * @since 2011-5-11
 */
@Controller("GetMetadataBusinessByPageAction")
@Scope("prototype")
public class GetMetadataBusinessByPageAction extends BaseAjaxAction<Map<String,Object>>{
    
	private Long metaDataTableId;
	
	@Autowired
	private MetadataEngineWebService metadataEngineWebService;
	
	private Page<PageShowBean> showPage = new Page<PageShowBean>();
	
	private Boolean searchType =false;
	
	@Override
	public String _execute() throws Exception {
		//searchString = new String(searchString.getBytes("utf-8"), "ISO-8859-1");
		if(!searchType){
			page = metadataEngineWebService.findSystemDataSimpleByPage(metaDataTableId, page, searchString, orderConditions, propertyFilters);
		}else{
			page = metadataEngineWebService.findSystemDataAdvanceByPage(metaDataTableId, propertyFilters, page, orderConditions);
		}
		
		List<MetaDataFieldDto> showItems = metadataEngineWebService.findShowTitleItemsByTableId(metaDataTableId);
		//获取主键名称
		String pkName = metadataEngineWebService.findPrimaryKeyName(metaDataTableId);
		
		List<PageShowBean> resultList = new ArrayList<PageShowBean>();
//		System.out.println(page.getTotalCount());
//		System.out.println(page.getResult().size());
//		for(Map<String,Object> m : page.getResult()){
//			System.out.print(m);
//		}
		if(page!=null){
			showPage.setPageNo(page.getPageNo());
			showPage.setPageSize(page.getPageSize());
			showPage.setTotalCount(page.getTotalCount());
			for(Map<String,Object> map : page.getResult()){
				PageShowBean showBean = new PageShowBean();
				showBean.setId((Long)map.get(pkName));
				showBean.setMetaDataTableId(metaDataTableId);
				Object[] fieldValues = new Object[showItems.size()];
				int i = 0;
				for(MetaDataFieldDto dto:showItems){
					fieldValues[i] = map.get(dto.getPrivateName());
					
					if("Date".equalsIgnoreCase(fieldValues[i].getClass().getSimpleName())){
						String simpleDate = DateTranslate.simpleDateToString((Date)fieldValues[i]);
						fieldValues[i] = simpleDate;
					}
					i++;
				}
				showBean.setFieldValues(fieldValues);
				resultList.add(showBean);
			}
			showPage.setResult(resultList);
			showPage.setAutoCount(page.isAutoCount());
		}
		
		return SUCCESS;
	}

	public Long getMetaDataTableId() {
		return metaDataTableId;
	}

	public void setMetaDataTableId(Long metaDataTableId) {
		this.metaDataTableId = metaDataTableId;
	}

	@Override
	public Map<String, Object> getDetailData() {
		return null;
	}

	public Page<PageShowBean> getShowPage() {
		return showPage;
	}

	public void setShowPage(Page<PageShowBean> showPage) {
		this.showPage = showPage;
	}

	public Boolean getSearchType() {
		return searchType;
	}

	public void setSearchType(Boolean searchType) {
		this.searchType = searchType;
	}

}
